package com.ruyiadmin.springboot.common_infrastructure.models.base_entity_model;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.annotation.*;
import com.ruyiadmin.springboot.common_infrastructure.classes.SysUserDTO;
import com.ruyiadmin.springboot.common_infrastructure.components.system.RuYiSessionContext;
import com.ruyiadmin.springboot.common_infrastructure.enums.business.YesNo;
import com.ruyiadmin.springboot.common_infrastructure.models.RuYiAdminBaseDomainEntity;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.hibernate.validator.constraints.Length;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.time.LocalDateTime;
import java.util.UUID;

/**
 * 实体基类
 *
 * @author RuYiAdmin
 * @since 2022-06-21
 */
@Data
public class RuYiAdminBaseEntity extends RuYiAdminBaseDomainEntity {
    @ApiModelProperty("模块编号")
    @TableId("ID")
    @NotNull
    private String id;

    @ApiModelProperty("备注")
    @TableField("REMARK")
    @Length(max = 512)
    private String remark;

    @ApiModelProperty("标志位")
    @TableField(value = "ISDEL", fill = FieldFill.INSERT)
    @TableLogic
    //@NotNull
    private Integer isdel;

    @ApiModelProperty("创建人")
    @TableField(value = "CREATOR", fill = FieldFill.INSERT)
    @NotNull
    private String creator;

    @ApiModelProperty("创建时间")
    @TableField(value = "CREATE_TIME", fill = FieldFill.INSERT)
    @NotNull
    private LocalDateTime createTime;

    @ApiModelProperty("修改人")
    @TableField(value = "MODIFIER", fill = FieldFill.INSERT_UPDATE)
    @NotNull
    private String modifier;

    @ApiModelProperty("修改时间")
    @TableField(value = "MODIFY_TIME", fill = FieldFill.INSERT_UPDATE)
    @NotNull
    private LocalDateTime modifyTime;

    @ApiModelProperty("版本号")
    @TableField(value = "VERSION_ID", fill = FieldFill.INSERT_UPDATE)
    //@NotNull
    @Version    // 乐观锁注解
    private String versionId;

    @Resource
    @TableField(exist = false)
    private RuYiSessionContext sessionContext;

    public void create() throws Exception {
        SysUserDTO user = sessionContext.getCurrentUserInfo();
        this.setId(UUID.randomUUID().toString());
        this.setIsdel(YesNo.NO.ordinal());
        this.setCreator(user.getId());
        this.setCreateTime(LocalDateTimeUtil.now());
        this.setModifier(user.getId());
        this.setModifyTime(LocalDateTimeUtil.now());
        this.setVersionId(UUID.randomUUID().toString());
    }

    public void modify() throws Exception {
        SysUserDTO user = sessionContext.getCurrentUserInfo();
        this.setModifier(user.getId());
        this.setModifyTime(LocalDateTimeUtil.now());
        this.setVersionId(UUID.randomUUID().toString());
    }

    public void delete() throws Exception {
        SysUserDTO user = sessionContext.getCurrentUserInfo();
        this.setIsdel(YesNo.YES.ordinal());
        this.setModifier(user.getId());
        this.setModifyTime(LocalDateTimeUtil.now());
        this.setVersionId(UUID.randomUUID().toString());
    }
}
